<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <title>角色列表</title>
    <script src="../../../static/kaige/js/szload.js"></script>
    <link rel="stylesheet" href="../../../static/kaige/layuiextend/dtree/dtree.css">
    <link rel="stylesheet" href="../../../static/kaige/layuiextend/dtree/font/dtreefont.css">
</head>

<body>
    <div class="kaige-body">
        <table class="layui-table" id="roleTable" lay-filter="roleTable" lay-size="sm"></table>
        <input type="hidden" name="keywordsCache" value="{}" />
    </div>
    <script>
        load_init_js(function () {
            layui.config({
                base: szoapcpath + '/static/kaige/layuiextend/'
            }).extend({
                dtree: 'dtree/dtree'
            }).use(['table', 'form', 'layer', 'dtree', 'util', 'kaige'], function () {
                var element = layui.element,
                    layer = layui.layer,
                    table = layui.table,
                    util = layui.util,
                    dtree = layui.dtree,
                    form = layui.form,
                    laytpl = layui.laytpl,
                    kaige = layui.kaige,
                    $ = layui.$;
                form.render();

                kaige.table({
                    elem: '#roleTable',
                    height: 'full-25',
                    url: "/sys.role/index",
                    id: 'roleTable',
                    toolbar: '#dtoolbar',
                    defaultToolbar: ['filter'],
                    cols: [[
                        { field: 'id', width: 90, title: 'ID', align: 'center' },
                        { field: 'name', width: 200, title: '角色名称', align: 'center' },
                        { field: 'create_time', width: 180, title: '创建时间', width: 160, align: 'center' },
                        { title: '操作', toolbar: '#dtrbar', align: 'center', width: 150, fixed: 'right' }
                    ]]
                });

                //列表-监听表头工具栏事件dtoolbar
                table.on('toolbar(roleTable)', function (obj) {
                    //var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'btn-add':
                            btnadd();
                            break;
                        case 'btn-search':
                            btnsearch();
                            break;
                    }
                });

                //列表-监听单元格行工具事件dtrbar
                table.on('tool(roleTable)', function (obj) {
                    var that = this;
                    var data = obj.data;
                    switch (obj.event) {
                        case 'auz':
                            barauz(data);
                            break;
                        case 'del':
                            bardel(data);
                    }
                });

                //添加
                function btnadd() {
                    kaige.form({
                        title: '<i class="layui-icon layui-icon-add-circle"></i> 添加角色'
                        , url: '/sys.role/add' //数据源
                        , data: {}
                        , id: 'kaige'
                        ,tablelayid: 'roleTable'
                        ,area: ['400px', '200px']
                        ,overflow: false
                    });
                }

                //角色删除
                function bardel(d) {
                    layer.confirm('您确定删除此角色吗？', {
                        title: '<i class="layui-icon layui-icon-delete"></i> 删除确认',
                        btn: ['确认', '取消']
                    }, function () {
                        $.post("/sys.role/del", {id: d.id }, function (res) {
                            res.status && table.reload('roleTable');
                            layer.msg(res.msg);
                        });
                    });
                };

                //角色权限设置
                function barauz(d) {
                    var id = d.id;
                    layer.open({
                        type: 1,
                        area: ['400px', '600px'],
                        content: '<ul id="roleTree" class="dtree" data-id="0"></ul>',
                        title: '<i class="layui-icon layui-icon-auz"></i> 配置权限',
                        btn: ['保存', '关闭'],
                        success: function (layero, index) {
                            var obj = $(layero).find("#roleTree");
                            DTree = dtree.render({
                                obj: obj,
                                initLevel: 1,
                                request: { "id": id }, // 角色ID
                                url: '/sys.role/setauz',
                                checkbar: true,
                                cache: true,
                                checkbarType: 'all-self',//选中类型
                                response: { message: "msg", statusCode: 0 },
                                dataStyle: "layuiStyle",
                            });
                        },
                        yes: function (index, layero) {
                            var thedata = dtree.getCheckbarNodesParam(DTree);
                            var ids = [], updata = {};
                            $.each(thedata, function (i, obj) {
                                ids.push(obj.basicData);
                            });
                            updata = {
                                'id': id,
                                'data': ids.join(',')
                            };
                            $.post('/sys.role/savePerm', updata, function (re) {
                                layer.msg(re.msg);
                                if (re.status) {
                                    layer.close(index);
                                }
                            });
                        }
                    });
                }
            });
        });
    </script>
    <script type="text/html" id="dtrbar">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-xs" lay-event="auz" title="权限"><i class="layui-icon layui-icon-auz"></i></button>
            <button class="layui-btn layui-btn-xs" lay-event="del" title="删除"><i class="layui-icon layui-icon-delete"></i></button>
        </div>
    </script>
    <script type="text/html" id="dtoolbar">
        <div class="layui-form" lay-filter="searchform">
            <div class="layui-btn-group">
                <button type="button" class="layui-btn layui-btn-sm" lay-event="btn-add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
            </div>
        </div>
    </script>
    <script id="kaige_form_html" type="text/html">
        <form class="kaige-body layui-form layui-form-pane" lay-filter="kaige_form_lay_filter">
        {{# if(d.id){ }}
        <input type="hidden" name="id" value="{{ d.id }}" />
        {{# } }}
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-block">
                <input name="name" value="{{ d.name|| '' }}" lay-verify="required" lay-reqtext="请输入角色名称" placeholder="请输入角色名称" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <button class="layui-hide" lay-submit="" id="kaige_form_btn_save" lay-filter="kaige_form_btn_save">保存数据</button>
        </div>
    </form>
</body>

</html>